﻿//Common.js should be used only for functions that are available (and needed) code-wide.
//None of page-specific functions should be placed here.
/// <reference path="jquery.intellisense.js"/>
var base = '';
var showLoading = true;
var gtInterval = 60 * 1000 //interval, MSec (1000 = 1 sec.)
$(function() {
    base = $('base').attr('href');
    $('body').append('<div id="Error"></div>').append('<div id="Loading"><img src="img/ajax-loader.gif" hspace="5" align="absmiddle"/>Загрузка</div>');
    $('body').ajaxError(function(e, r, s) { if (r.status == 403) { alert('У вас нет прав на совершение этой операции, или закончилась сессия. Пройдите авторизацию заново.'); top.location.href = base; } });
    $('#Loading').ajaxStart(function() { showLoading = true; setTimeout('if(showLoading) { $("#Loading").show(); }', 300); }).ajaxStop(function() { $(this).hide(); showLoading = false; });
    
    if ($('.jclock'))
        setInterval(setTime, gtInterval);
});

function setTime()
{
   $.get(base+'login.ivp/getcurrenttime', {timestamp: new Date()}, function(data) {$('.jclock').html(data);});  
}

function getId(id)
{
///<summary>
///Returns numeric value from id string like 'SomeText_id'
///</summary>
    if(!id) return null;
    id = id.toString(); 
    if(id.indexOf('_')<0) return id;
    return id.substr(id.lastIndexOf('_')+1);
}

function getPrefix(id)
{
///<summary>
///Returns prefix from id string like 'SomeText_id'
///</summary>
    if(!id) return;
    id = id.toString();
    if(id.indexOf('_')<0) return id;
    return id.substr(0, id.indexOf('_'));
}

function getControllerByUrl(url)
{
///<summary>
///Allows to get controller by url like 'controller/action/params?other_params'
///</summary>
    url = url.toString();
    if( url.indexOf('/') < 0 ) return url;
    return url.substr(0,url.indexOf('/'));
}

function fillFormat(format, obj)
{
    ///<summary>
    ///Returns string specified in format with wildcards like %id replaced with properties of specified object.
    ///</summary>
    var re = /(\%[\w}]+)/g;
    var matches = format.match(re);
    $(matches).each(function(i,m){ var val=obj[m.substr(1)]; if(!val) val=''; format = format.replace(m,val); });
    return format;
}

function reloadUL(ul,objects)
{
    ///Reloads UL with list of objects
    $(ul).html('');
    var format = ($(ul).attr('itemformat')) ? $(ul).attr('itemformat') : '<li>%Name</li>';
    var html = '';
    for(var i=0;i<objects.length;i++)
        html += fillFormat(format,objects[i]);
    $(ul).html(html);
}

function addCheckListWarning(cls,id)
{
    ///Shows warning if one or more of checkboxes with specified class are not checked
    $('.'+cls).click(function(){ var show = false; $('.'+cls).each(function(i,el){ if( !el.checked ) show = true; }); if( show ) $('#'+id).show(); else $('#'+id).hide(); });
}

function openDialog(url, name, params) {
///Opens window with the specified url, name and params, but without radio, toolbar etc.
    if (!url) return;
    var width = (params && params.width) ? params.width: 860;
    var height = (params && params.height) ? params.height : 600;
    var features = 'width=' + width + ',height=' + height + ',location=0,radio=0,toolbar=0,status=0,resizable=1,scrollbars=1';
    if (params && params.menubar) features += ',menubar=' + params.menubar;
    return window.open(base+url, name, features, false);
}

function toggle(link, id) {
    ///Toggles element with specified id and changed link text
    if ($(id).is(':visible')) {
        $(id).hide('fast');
        $(link).html('&darr; Показать');
    }
    else {
        $(id).show('fast');
        $(link).html('&uarr; Скрыть');
    }
    return false;
}

function setTab(id) {
    if (!id) return;
    $('#tab').val(id);
    $('.tab').hide();
    $('#'+id).show();
    $('#tabs>ul>li>a').removeClass('selected');
    $('#tabs>ul>li>a#a' + id).addClass('selected');
    return false;
}

function leadZero(number,len) {
    while (number.length < len)
        number = '0' + number;
    return number;
}